package com.tony.business.controller;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.ShearCaptcha;
import com.alibaba.fastjson.JSON;
import com.tony.business.service.AdminUserService;
import com.tony.business.util.RedisUtil;
import com.tony.business.util.TokenUtil;
import com.tony.domain.bo.JsonResult;
import com.tony.domain.bo.UserInfo;
import com.tony.domain.exception.CustomException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotBlank;
import java.io.IOException;

/**
 * @author tony
 * @date 2020/6/14 12:01
 */
@Api(tags = "登录相关")
@RestController
public class TokenController extends BaseController {

    @Autowired
    AdminUserService adminUserService;
    @Autowired
    RedisUtil redisUtil;

    @PostMapping("/login")
    @ApiOperation(value = "登录", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "用户名", paramType = "query", required = true),
            @ApiImplicitParam(name = "password", value = "密码", paramType = "query", required = true),
            @ApiImplicitParam(name = "imgCode", value = "验证码", paramType = "query", required = true),
            @ApiImplicitParam(name = "uuid", value = "uuid", paramType = "query", required = true),
    })
    public JsonResult<String> login(@NotBlank String name, @NotBlank String password, @NotBlank String imgCode, @NotBlank String uuid) {
        Object code = redisUtil.get(uuid);
        if (code == null) {
            throw new CustomException("验证码为空");
        }
        if (!code.equals(imgCode)) {
            throw new CustomException("验证码错误");
        }
        UserInfo userInfo = adminUserService.login(name, password);
        String token = TokenUtil.createToken(userInfo);
        redisUtil.set(token, JSON.toJSONString(userInfo), 45);
        return new JsonResult<>(token);
    }

    @GetMapping(value = "/imgCode/{uuid}")
    @ApiOperation(value = "验证码", httpMethod = "GET")
    public void imgCode(@PathVariable(value = "uuid") String uuid) throws IOException {
        //定义图形验证码的长、宽、验证码字符数、干扰元素个数
        ShearCaptcha captcha = CaptchaUtil.createShearCaptcha(210, 100, 4, 4);
        String code = captcha.getCode();
        response.setContentType(MediaType.IMAGE_PNG_VALUE);
        captcha.write(response.getOutputStream());
        response.flushBuffer();
        redisUtil.set(uuid, code, 5);
    }

    @PostMapping("/refreshToken")
    @ApiOperation(value = "刷新token", httpMethod = "POST")
    public JsonResult<String> refreshToken() {
        String token = request.getHeader("token");
        Object jsonUserInfo = redisUtil.get(token);
        if (jsonUserInfo == null) {
            throw new CustomException("token为空或已超时");
        }
        redisUtil.delete(token);
        String newToken = TokenUtil.refreshToken(token);
        redisUtil.set(newToken, jsonUserInfo, 45);
        return new JsonResult<>(newToken);
    }
}
